<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/18
 * Time: 16:23
 */

namespace app\admin\controller\shop;


use app\admin\controller\Upload;
use think\Controller;
use think\Db;
//use think\{Controller,Db};

class Adv extends Controller
{
    public function edit(){
        $model=new \app\admin\model\Adv();
        if($this->request->isPost()){
            $data=$this->request->param();
            $id=$this->request->has("id");


            if($this->request->has("file","file")){//有文件上传
                $res=(new Upload())->uploadOne("file");
                if($res["code"]==200){
                    $url=str_replace("\\","/",$res["url"]);
                    $data["thumb"]=$url;
                }else{
                    $this->error($res["errmsg"]);
                }
            }


            if($id){//更新
                Db::name("ewei_shop_adv")->update($data);
            }else{//插入
                $model->name("ewei_shop_adv")->insert($data);
            }
            $this->redirect("page");
        }else{
            if($this->request->has("id")){
                $id=$this->request->param("id");
                $item=$model->name("ewei_shop_adv")
                    ->where("iswxapp","=",0)
                    ->where("uniacid","=",1)
                    ->where("id",$id)
                    ->find();
                $this->assign("item",$item);
            }else{

            }
            $this->assign("title","添加幻灯片");
            return $this->fetch();
        }

    }

    public function delete(){
        $model=new \app\admin\model\Adv();
        if($this->request->isGet()){
            $id=$this->request->param('id');
            $res=$model->name("ewei_shop_adv")->insert([
                "id"            =>$id,
                "iswxapp"       =>1
            ],true);
            if($res){
                $this->success("删除成功");
            }else{
                $this->error("删除失败");
            }
        }else{
            $data=$this->request->param();
            /*
            foreach ($data["data"] as $key  =>$value){
                $res=$model->name("ewei_shop_adv")->insert($value,true);
            }
            */
            $model->name("ewei_shop_adv")->insertAll($data["data"],true);
            return json([
                "code"          =>200,
                "msg"       =>"success"
            ]);
        }

    }

    public function add(){
        return  $this->fetch();
    }

    public function page(){
        $model=new \app\admin\model\Adv();
        $item=$model->name("ewei_shop_adv")
            ->where("iswxapp","=",0)
            ->where("uniacid","=",1)
            ->order("displayorder","desc")
            ->paginate(10);
        $page=$item->render();
        $this->assign("page",$page);
        $this->assign("item",$item);
        return $this->fetch("index");
    }

    public function search(){
        $model=new \app\admin\model\Adv();
        $post=$this->request->param();
        $content=$post['content'];
        $enabled=$post['enabled'];
        $item=$model->name("ewei_shop_adv")
            ->where([
                "iswxapp"       =>0,
                "uniacid"       =>1
            ])->order("displayorder","desc")
            ->where("enabled",$enabled)
            ->whereLike( "advname",'%'.$content.'%',"AND")
            ->paginate(10);
        $page=$item->render();
        $this->assign("page",$page);
        $this->assign("item",$item);
        return $this->fetch("index");
    }

    public function enabled(){
        $data=$this->request->param();
        if($this->request->has("data")){
            foreach ($data["data"] as $key =>$value){
                $res=Db::name("ewei_shop_adv")->update($value);
            }
        }else{
            $res=Db::name("ewei_shop_adv")->update($data);
        }


        if(!$res){
            return json([
                "errcode"       =>3000,
                "errmsg"        =>$res
            ]);
        }
        return json([
            "code"          =>200,
            "msg"           =>"success"
        ]);
    }

}